
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using Swaf;
using Swaf.Gui;
using Swaf.Gui.AspNet2;

namespace Syrinx.Gui.AspNet.NitobiCtls
{
	public class NitobiPaginationDisplay : BasicPaginationDisplay, INitobiClientControl
	{
		protected static string s_setupScript = @"
<script>
var cc = new ClientControl(document.getElementById('{0}'), setPaginationState, getPaginationState);
cc.id = '{0}';
{1}BizProc.addClientControl(cc);
</script>
";
		protected static string s_globalScript = @"
function setPaginationState(stateEl)
{
	try
	{
		var html = stateEl.getAttribute('html');
		if(html != null && html != '')
			this.dhtmlObj.innerHTML = html;
	}catch(e){}
}
function getPaginationState(stateEl)
{
}
";

		public NitobiPaginationDisplay() : base() { }
		public NitobiPaginationDisplay(ActiveWidget widget) : base(widget) { }

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			if (Page is INitobiPage || (Page is NitobiBasePage && ((NitobiBasePage)Page).UsingAjax))
				Page.ClientScript.RegisterClientScriptBlock(GetType(), "Global", s_globalScript, true);
		}

		protected override void Render(System.Web.UI.HtmlTextWriter w)
		{
			base.Render(w);
			ActiveBizProc bizproc = BPC.getActiveBizProc(State.ParentBizProcIds[0]);

			w.Write(s_setupScript, ClientID, bizproc.BizProc.Id);
		}

		#region INitobiClientControl Members
		public bool CreateOnLoad { set { m_createActiveOnLoad = value; } }


		public void setState(System.Xml.XmlElement clientState)
		{
		}

		public void getState(System.Xml.XmlElement clientState, string pageHtml)
		{
			StringWriter w = new StringWriter();
			renderIntoWriter(w);
			clientState.SetAttribute("html", w.ToString());
		}

		public void processAjaxEvent(System.Xml.XmlElement eventInfo)
		{
			string action = eventInfo.GetAttribute("event");
			//Getting bizproc is just so it gets activated and processed.
			ActiveBizProc bizproc = BPC.getActiveBizProc(State.ParentBizProcIds[0]);
			setupCurrentPage(action);
		}

		public bool NeedToGenerateHtmlWithServerControls
		{
			get { return false; }
		}

		#endregion

		protected override string genPostBack(string pInfo)
		{
			if (Page is INitobiPage || (Page is NitobiBasePage && ((NitobiBasePage)Page).UsingAjax))
			{
				ActiveBizProc bizproc = BPC.getActiveBizProc(State.ParentBizProcIds[0]);
				return string.Format(@"{0}BizProc.triggerEvent('{1}','{2}');", bizproc.BizProc.Id, WidgetId, pInfo);
			}
			else
				return base.genPostBack(pInfo);
		}
	}
}
